
package com.suarte.core.hibernate;
import com.suarte.core.Company;
import com.suarte.core.Contact;
import com.suarte.core.WorkOrder;
import com.suarte.core.WorkOrderDetail;
import com.suarte.core.dao.WorkOrderDao;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.appfuse.dao.hibernate.GenericDaoHibernate;

import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

/**
 * @date   Dec 20, 2010
 * @author Ggutierrez
 */
@Repository("workOrderDao")
public class WorkOrderDaoHibernate extends GenericDaoHibernate<WorkOrder, Long> implements WorkOrderDao {
    
    public WorkOrderDaoHibernate() {
        super(WorkOrder.class);
    }

    public List<WorkOrder> findByDescription(String description) {
        return getHibernateTemplate().find("from WorkOrder where description like upper(?)", description.concat("%").toUpperCase());
    }

    @Transactional(readOnly = false, propagation = Propagation.REQUIRES_NEW)
    public void store(WorkOrder object) {
        getHibernateTemplate().saveOrUpdate(object);
    }

    @Override
    public WorkOrder get(Long id) {
        List<WorkOrder> results = getHibernateTemplate().find("from WorkOrder this join fetch this.details where this.id = ?", id);
        return !results.isEmpty() ? results.get(0) : null;
    }

    public void deleteDetails(List<WorkOrderDetail> args) {
        getHibernateTemplate().deleteAll(args);
    }

    public List<WorkOrder> findOrders(Long id, Long quotationId, String description, String user, Date date, Company company, Contact contact){
        String hql = "from WorkOrder this join fetch this.employee employee left join fetch employee.user user  " +
                     " join fetch this.quotation quotation " +
                     " join fetch quotation.contact contact " +
                     " where 1 = 1";
        List args = new ArrayList();
        
        if (id != null){
            args.add(id);
            hql += " and this.id = ?";
        }

        if (quotationId != null){
            args.add(quotationId);
            hql += " and quotation.id = ?";
        }

        if (!isBlank(description)) {
            args.add(description.toUpperCase().concat("%"));
            hql += " and upper(ltrim(rtrim(this.description))) like upper(ltrim(rtrim(?)))";
        }
        if (!isBlank(user)) {
            args.add(user);
            hql += " and upper(ltrim(rtrim(user.username))) like upper(ltrim(rtrim(?)))";
        }
        if (date != null) {
            args.add(date);
            hql += " and (this.receptionDate) = (?)";
        }

        if (company != null) {
            args.add(company);
            hql += " and contact.company = ?";
        }

        if (contact != null) {
            args.add(contact);
            hql += " and contact = ?";
        }

        List results = getHibernateTemplate().find(hql, args.toArray());
        return results;
    }

    private boolean isBlank(String value) {
        return value == null || value.trim().length() == 0;
    }
}
